package mamikeji.liaowa.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.application.MyApplication;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MD5Util;

public class FaceActivity extends BaseActivity {
    private static final String TAG = "FaceActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        //隐藏标题栏以及状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Intent intent = getIntent();
        String scheme = intent.getScheme();
        System.out.println("schemescheme："+scheme);

        init();
    }

    private void init() {
        //记录app启动次数
        int num = ForPreference.getInt(ForContents.APP_COUNT);
        ForPreference.putInt(ForContents.APP_COUNT, ++num);
        getFirst();
    }

    public void getFirst() {
        if (ForPreference.getString(ForContents.GET_ANDROID_ID).equals("") || ForPreference.getString(ForContents.GET_ANDROID_ID) == null) {
            ForPreference.putString(ForContents.GET_ANDROID_ID, Build.SERIAL);
        }
        //授权
        requestRight();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 避免Handler导致内存泄漏
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 授权
     */
    public void requestRight() {
        final String androidID = ForPreference.getString(ForContents.GET_ANDROID_ID);
        ForLog.i("androidIDandroidIDandroidID", "" + androidID);
        String md5_ID = null;
        try {
            md5_ID = MD5Util.md5Encode(androidID + ForContents.SUFFIX);
        } catch (Exception e) {
            e.printStackTrace();
            ForToast.showToast("授权失败！请重新启动APP");
        }

        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.EQUIPMENTID, androidID);
        formMap.put(HttpKey.SOURCE, HttpKey.ANDROID);
        formMap.put(HttpKey.K, md5_ID);
        OkHttpUtils.post()
                .url(HttpApi.ROOT_PATH + HttpApi.LOGIN_RIGHT)
                .params(formMap)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        //跳转到错误标识页面
                        startActivity(new Intent(FaceActivity.this, AppErrorActivity.class));
                        finish();

                    }

                    @Override
                    public void onResponse(String response) {
                        ForLog.i(TAG, response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString(HttpKey.STATUS);
                            if ("200".equals(status)) {
                                ForPreference.putString(ForContents.USER_TOKEN, jsonObject.getString(ForContents.USER_TOKEN));
                                ForPreference.putString(HttpKey.EQUIPMENTID, androidID);

                                Boolean isFirstRun = ForPreference.getBoolean(ForContents.GET_FIRST);
                                if (isFirstRun) {
                                    startActivity(new Intent(FaceActivity.this, FistActivity.class));
                                } else {
                                    startActivity(new Intent(FaceActivity.this, WelcomeActivity.class));
                                }
                                finish();
                            } else {
                                //跳转到错误标识页面
                                startActivity(new Intent(FaceActivity.this, AppErrorActivity.class));
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
